package com.decent.ejfadmin.monitor.service.impl;

import cn.decent.component.shiro.entity.Admin;
import cn.decent.component.shiro.service.AdminService;
import com.decent.ejfadmin.common.bean.OrderMonitor;
import com.decent.ejfadmin.common.bean.SupplyCardMessage;
import com.decent.ejfadmin.common.enums.SupRedisKey;
import com.decent.ejfadmin.monitor.dao.MonitorDao;
import com.decent.ejfadmin.monitor.response.SupplyCardFace;
import com.decent.ejfadmin.monitor.response.SupplyCardSpeed;
import com.decent.ejfadmin.monitor.response.SupplyCardUser;
import com.decent.ejfadmin.monitor.service.MonitorService;
import com.decent.ejfadmin.utils.SimpleCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 监控
 *
 * @author weicx
 * @date 2018/08/21
 */
@Slf4j
@Service
public class MonitorServiceImpl implements MonitorService {
    @Autowired
    private MonitorDao monitorDao;
    @Autowired
    private AdminService adminService;

    /**
     * 底部监控
     *
     * @return 返回
     */
    @Override
    public OrderMonitor queryOrderTotal() {
        String startTime = DateFormatUtils.format(DateUtils.addDays(new Date(), -6), "yyyy-MM-dd 00:00:00");
        OrderMonitor orderMonitor = monitorDao.queryTotal(startTime);
        if (Objects.isNull(orderMonitor)) {
            orderMonitor = new OrderMonitor();
        }
        startTime =  DateFormatUtils.format(new Date(), "yyyy-MM-dd 00:00:00");
        orderMonitor.setTransferAbnormal(monitorDao.queryTransferAbnormalCount(startTime));
        return orderMonitor;
    }

    /**
     * 从缓存读取新批次卡
     *
     * @return 返回信息
     */
    @Override
    public SupplyCardMessage getCacheData() {
        try {
            // 读取缓存中提示信息
            Admin adminUser = adminService.getCurrentAdmin();
            SupplyCardMessage supplyCard = (SupplyCardMessage) SimpleCacheUtil.getObjectJson(SupRedisKey.SYS_ALERT.toString() + adminUser.getId(), SupplyCardMessage.class);
            SimpleCacheUtil.removeObject(SupRedisKey.SYS_ALERT.toString() + adminUser.getId());
            return supplyCard;
        } catch (Exception e) {
            log.warn("从缓存读取新批次卡异常[{}]", e);
            return null;
        }
    }

    /**
     * 销卡速度监控
     *
     * @return 返回
     */
    @Override
    public List<SupplyCardSpeed> getSupplyCardSpeed() {
        return monitorDao.getSupplyCardSpeed();
    }

    /**
     * 销卡面值监控
     *
     * @return 返回
     */
    @Override
    public List<SupplyCardFace> getSupplyCardFace() {
        return monitorDao.getSupplyCardFace(DateFormatUtils.format(new Date(), "yyyy-MM-dd 00:00:00"));
    }

    /**
     * 提卡用户监控
     *
     * @return 返回
     */
    @Override
    public SupplyCardUser getSupplyCardUser() {
        Date date = new Date();
        String startTime = DateFormatUtils.format(date, "yyyy-MM-dd 00:00:00");
        int successTotal = monitorDao.getSuccessTotal(startTime);
        List<String> newUserTotal = monitorDao.getNewSupplyCardUserTotal(startTime);
        StringBuffer userIds = new StringBuffer(newUserTotal.size() * 7);
        for (int i = 0; i < newUserTotal.size(); i++) {
            userIds.append(newUserTotal.get(i)).append(",");
        }
        userIds.append("0");
        SupplyCardUser supplyCardUser = new SupplyCardUser();
        supplyCardUser.setSuccessUserTotal(successTotal);
        supplyCardUser.setUserIds(userIds.toString());
        supplyCardUser.setNewUserTotal(newUserTotal.size());
        return supplyCardUser;
    }


}
